package com.iotechn.unimall.admin.api.leaseOrderDetails;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.LeaseOrderDetailsDO;
import com.iotechn.unimall.data.mapper.LeaseOrderDetailsMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
public class AdminLeaseOrderDetailsServiceImpl implements AdminLeaseOrderDetailsService {
    @Autowired
    private LeaseOrderDetailsMapper leaseOrderDetailsMapper;

    @Override
    public Page<LeaseOrderDetailsDO> list(Integer page, Integer limit, Long adminId) throws ServiceException {
        Wrapper<LeaseOrderDetailsDO> wrapper = new EntityWrapper<LeaseOrderDetailsDO>();
        /**if (!StringUtils.isEmpty(name)) {
         wrapper.like("username", name);
         }**/
        wrapper.orderBy("id", false);
        Integer count = leaseOrderDetailsMapper.selectCount(wrapper);
        List<LeaseOrderDetailsDO> LeaseOrderDetailsDOS = leaseOrderDetailsMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(LeaseOrderDetailsDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(LeaseOrderDetailsDO leaseOrderDetailsDO, Long adminId) throws ServiceException {
        Long id = leaseOrderDetailsDO.getId();
        if (id == null) return "fail";
        if (leaseOrderDetailsMapper.updateById(leaseOrderDetailsDO) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public String delete(Long id, Long adminId) throws ServiceException {
//        if (leaseOrderDetailsMapper.deleteById(id) > 0) {
//            return "ok";
//        }
//        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
//
//    }

    @Override
    public LeaseOrderDetailsDO detail(Long id, Long adminId) throws ServiceException {
        return leaseOrderDetailsMapper.selectById(id);
    }

//    @Override
//    public Boolean updateStatus(Long adminId, Long id, Integer status) throws ServiceException {
//        if (status == null || (status != 0 && status != 1)) {
//            throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
//        }
//        LeaseOrderDetailsDO leaseOrderDetailsDO = new LeaseOrderDetailsDO();
//        leaseOrderDetailsDO.setId(id);
//        leaseOrderDetailsDO.setStatus(status);
//        leaseOrderDetailsDO.setGmtUpdate(new Date());
//        if (leaseOrderDetailsMapper.updateById(leaseOrderDetailsDO) > 0) {
//            return true;
//        }
//        return false;
//    }

}
